package com.dhh.sky.widget;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.dhh.sky.R;
import com.dhh.sky.activity.FilePickActivity;
import com.dhh.sky.model.SkyDriveObject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class FilePickerListAdapter extends BaseAdapter {
    private final LayoutInflater mInflater;
    private FilePickActivity context;
    private File[] mFiles;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private ArrayList<File> items;
    private ArrayList<CheckBox> boxes;
	private File folder;

	public FilePickerListAdapter(FilePickActivity context){
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        items = new ArrayList<File>();
        boxes = new ArrayList<CheckBox>();
    }
    
    public void loadFile(){
    	mFiles = folder.listFiles();
    	items.clear();
    	boxes.clear();
    }

    public File[] getFiles() {
        return mFiles;
    }

    @Override
    public int getCount() {
        return mFiles==null?0:mFiles.length;
    }

    @Override
    public File getItem(int position) {
        return mFiles[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	final View view = convertView != null ? convertView : mInflater.inflate(R.layout.file_list_item,parent,false);
		CheckBox check = (CheckBox) view.findViewById(R.id.fli_check);
		ImageView icon = (ImageView) view.findViewById(R.id.fli_icon);
		TextView name = (TextView) view.findViewById(R.id.fli_name);
		TextView detail = (TextView) view.findViewById(R.id.fli_detail);

        final File file = getItem(position);
        name.setText(file.getName());
        Date mDate = new Date();
        mDate.setTime(file.lastModified());
        String date = format.format(mDate);
        String size = readableByteCount(file.length(),true);
        detail.setText(size+" - "+date);
        if(file.isDirectory()){
        	icon.setImageResource(R.drawable.ic_nonemptydocumentfolder);
        	check.setEnabled(false);
        }else{
        	icon.setImageResource(SkyDriveObject.getFileIcon(getFileExtesion(file.getName())));
        	check.setEnabled(true);
        }
        
        check.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View view) {
				CheckBox check = (CheckBox) view;
				if(!check.isChecked()){			    	
			    	for(int i=0; i<items.size(); i++){
			    		File sf = items.get(i);
			    		if(sf.getName().equals(file.getName())){
			    			items.remove(i);
			    		}
			    	}	
				}else{
					File file = getItem(position);			    	
			    	items.add(file);	
				}	    	
		    	context.modeChanged();
			}
		});
        boxes.add(check);
        return view;
    }
    
    public void unSelectFiles(){
    	items.clear();
    	for(CheckBox check : boxes){
    		if(check!=null)
    			check.setChecked(false);
    	}
    }
    
    private String readableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }
    
    private String getFileExtesion(String fileName){
    	String ext = "";
    	int dotIndex=fileName.lastIndexOf('.');
    	if(dotIndex>=0) {
    		ext=fileName.substring(dotIndex+1,fileName.length());
    	}
    	return ext;
    }

    public void setFolder(File folder) {
		this.folder = folder;
	}
    
    public File getFolder() {
		return folder;
	}
    
    public boolean hasParent(){
    	boolean flag = false;
    	if(folder!=null && folder.getParentFile()!=null){
    		flag = true;
    	}
    	return flag;
    }
    
    public void selectFile(View view,int position){
    	CheckBox check = (CheckBox) view.findViewById(R.id.fli_check);
    	File file = getItem(position);
    	
    	for(int i=0; i<items.size(); i++){
    		File sf = items.get(i);
    		if(sf.getName().equals(file.getName())){
    			items.remove(i);
    			check.setChecked(false);
    			context.modeChanged();
    			return;
    		}
    	}
    	    	
    	items.add(file);
		check.setChecked(true); 	
    	
    	context.modeChanged();
    }
    
    public int getSelectCount(){
    	return items==null ? 0:items.size();
    }
    
    public List<File> getItemSelected(){
    	return items;
    }
}
